package com.chtw.oasystem.controller;

import com.chtw.oasystem.bean.FullUserInfo;
import com.chtw.oasystem.common.response.RespUtils;
import com.chtw.oasystem.respBean.ListChangeLogsResp;
import com.chtw.oasystem.service.impl.ChangeLogServiceImpl;
import com.chtw.oasystem.utils.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author chtw
 * @date 2020-11-18 23:35
 */
@RestController
@ResponseBody
@RequestMapping("/log")
public class ChangeLogsController {

    @Autowired
    private ChangeLogServiceImpl changeLogService;

    private static final Logger logger = LoggerFactory.getLogger(ChangeLogsController.class);

    @GetMapping("/list")
    public RespUtils<ListChangeLogsResp> list(@RequestParam(defaultValue = "1", required = false) int page, @RequestParam(defaultValue = "10", required = false) int limit, HttpSession session) {
        try {
            FullUserInfo fullUserInfo = Session.getCurrentUser(session);
            if (fullUserInfo == null) {
                return RespUtils.fail("用户未登录！");
            } else if (fullUserInfo.getRoleId() < 4) {
                return RespUtils.success(changeLogService.list(page, limit, fullUserInfo));
            } else {
                return RespUtils.fail("没有权限");
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }
}
